// map.js
import qqMap from '../../../utils/qqMap'
let repair_id = 0
Page({
  data: {
    dialogVisible: false,
    latitude: 40.060539,
    longitude: 116.343847,
    // 标记点
    markers:[
      // 云趣园
     {
       id: 1,
       latitude: 40.086757,
       longitude: 116.328634,
       width: 24,
       height: 30
     },
     // 育知路
     {
       id: 2,
       latitude: 40.08346500000002,
       longitude: 116.33293800000011,
       iconPath: '/static/images/marker.png',
       width: 40,
       height: 40
     },
    //  传智播客
     {
      id: 3,
      latitude: 40.060539,
      longitude: 116.343847,
      width:24,
      height:30
    }
   ],
   // 线路
    polyline: [{
      points: [
        { latitude: 40.08346500000002, longitude: 116.33293800000011 }, // 云趣园
        { latitude: 40.086757, longitude: 116.328634 }, // 育知路
      ],
      color: '#FF0000DD',
      width: 4
    }]
  },
  // 确认提示框关闭时触发
  async dialogClose(ev) {
    console.log(ev.detail)
    // 点击了确定
    if(ev.detail === 'confirm'){
    // 调用取消接口
    const {code} = await wx.http.put(`/cancel/repaire/${repair_id}`)
    if(code !== 10000) return wx.utils.toast()
    wx.navigateBack({})
    }
  },
  onLoad({id}){
    repair_id = id
    this.getRepairDetail(id)
    this.createPolyline()
  },
  // 获取报修详情数据
  async getRepairDetail(id){
    const {code,data} = await wx.http.get(`/repair/${id}`)
    if(code !== 10000) return wx.utils.toast()
    // 请求成功
    this.setData({
      ...data
    })
  },
  // 生成线路
  createPolyline(){
    qqMap.direction({
      mode:'bicycling',
      from:'40.060539,116.343847', // 传智播客
      to: '40.086757,116.328634', // （示例）云趣园1区
      success:(res) => {
        console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        this.setData({
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      }
    })
  },
  // 取消报修
  openDialogLayer(){
    this.setData({
      dialogVisible:true
    })
  },
  // 点击修改信息
  goEditRepair(){
    // 跳转到编辑页面 并传id
    wx.navigateTo({
      url:'/repair_pkg/pages/form/index?id=' + repair_id
    })
  }
})
